package com.smugmugsoftware.airconcalc.advanced;

import android.app.AlertDialog;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class CompassSelector extends Spinner {
	
	final private Spinner thisSpinner = this;
	private AlertDialog compassDialog;
	private boolean clicked;

	public CompassSelector(Context context) {
		super(context);
		initialize();
	}

	public CompassSelector(Context context, int mode) {
		super(context, mode);
		initialize();
	}

	public CompassSelector(Context context, AttributeSet attrs) {
		super(context, attrs);
		initialize();
	}

	public CompassSelector(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initialize();
	}

	public CompassSelector(Context context, AttributeSet attrs, int defStyle, int mode) {
		super(context, attrs, defStyle, mode);
		initialize();
	}


	private void initialize() {
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_item);
		String[] facingString = getResources().getStringArray(R.array.facing); 
		for (int i = 0; i < facingString.length; i++) {
			adapter.add(facingString[i]);
		}
		setAdapter(adapter);
		
		setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
				if (compassDialog != null) {
					compassDialog.dismiss();
				}
			}

			public void onNothingSelected(AdapterView<?> parent) {
				if (compassDialog != null) {
					compassDialog.dismiss();
				}				
			}
		});
	}

	@Override
	public boolean performClick() {
		clicked = false;

		AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getContext());
		LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View dialogView = inflater.inflate(R.layout.compass_selector, null);

		TextView N = (TextView) dialogView.findViewById(R.id.N);
		N.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				thisSpinner.setSelection(0);
				clicked = true;
				compassDialog.dismiss();
			}
		});
		TextView NE = (TextView) dialogView.findViewById(R.id.NE);
		NE.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				thisSpinner.setSelection(1);
				clicked = true;
				compassDialog.dismiss();
			}
		});
		TextView E = (TextView) dialogView.findViewById(R.id.E);
		E.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				thisSpinner.setSelection(2);
				clicked = true;
				compassDialog.dismiss();
			}
		});
		TextView SE = (TextView) dialogView.findViewById(R.id.SE);
		SE.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				thisSpinner.setSelection(3);
				clicked = true;
				compassDialog.dismiss();
			}
		});
		TextView S = (TextView) dialogView.findViewById(R.id.S);
		S.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				thisSpinner.setSelection(4);
				clicked = true;
				compassDialog.dismiss();
			}
		});
		TextView SW = (TextView) dialogView.findViewById(R.id.SW);
		SW.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				thisSpinner.setSelection(5);
				clicked = true;
				compassDialog.dismiss();
			}
		});
		TextView W = (TextView) dialogView.findViewById(R.id.W);
		W.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				thisSpinner.setSelection(6);
				clicked = true;
				compassDialog.dismiss();
			}
		});
		TextView NW = (TextView) dialogView.findViewById(R.id.NW);
		NW.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				thisSpinner.setSelection(7);
				clicked = true;
				compassDialog.dismiss();
			}
		});

		dialogBuilder.setView(dialogView);
		compassDialog = dialogBuilder.create();
		compassDialog.show();
		return clicked;
	}
	
}
